package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;
	@Autowired
	private VodService vodService;


	// 获取专辑声音分页列表
	@GuiguLogin(required = false)
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(
			@PathVariable Long albumId,
			@PathVariable Long page,
			@PathVariable Long limit) {
		//用户id
		Long userId = AuthContextHolder.getUserId();
		Page<AlbumTrackListVo> pageParam = new Page(page,limit);
		IPage<AlbumTrackListVo> pageModel = trackInfoService.findAlbumTrackPage(pageParam,albumId,userId);
		return Result.ok(pageModel);
	}


	@Operation(summary = "上传声音")
	@PostMapping("uploadTrack")
	public Result<Map<String, Object>> uploadTrack(MultipartFile file){
		Map<String, Object>  map = vodService.uploadTrack(file);
		return Result.ok(map);

	}
	@Operation(summary = "保存声音")
	@PostMapping("saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){
	 	 trackInfoService.saveTrackInfo(trackInfoVo);
		  return Result.ok();
	}
	@GuiguLogin(required = true)
	@Operation(summary = "获取当前用户声音分页列表")
	@PostMapping("findUserTrackPage/{page}/{limit}")
	public Result<IPage<TrackInfo>> findUserTrackPage(@PathVariable Long page,
													  @PathVariable Long limit,
													  @RequestBody TrackInfoQuery trackInfoQuery){
		//设置当前用户 id
		trackInfoQuery.setUserId(1L);
		//创建对象

		Page<TrackInfo> pageParam = new Page<>(page, limit);
		IPage<TrackInfo> trackListVoIPage =
		trackInfoService.findUserTrackPage(pageParam, trackInfoQuery);
		return Result.ok(trackListVoIPage);
	}
	@DeleteMapping("removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	@Operation(summary = "获取声音信息")
	@GetMapping("getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id){
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	@Operation(summary = "修改声音信息")
	@PutMapping("updateTrackInfo/{id}")
	public Result<TrackInfo> updateTrackInfo(@PathVariable Long id ,@RequestBody @Validated TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(id,trackInfoVo);
		return Result.ok();

	}

}

